package com.feeyo.location

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import com.blankj.utilcode.util.Utils


/**
 *作者：QLH on 2020-05-15
 *描述：
 */
internal class BackgroundLocationNotify {

    private val decripe = "定位前台服务"
    private var notificationManager: NotificationManager? = null
    private val channelId = "service_channel_location"
    var isCreateChannel = false

    fun buildNotification(): Notification {
        val builder: NotificationCompat.Builder

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Android O上对Notification进行了修改，如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
            if (null == notificationManager) {
                notificationManager =
                    Utils.getApp()
                        .getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            }
            if (!isCreateChannel) {
                val notificationChannel = NotificationChannel(
                    channelId,
                    decripe, NotificationManager.IMPORTANCE_LOW
                )
                notificationChannel.enableLights(true) //是否在桌面icon右上角展示小圆点
                notificationChannel.lightColor = Color.BLUE //小圆点颜色
                notificationChannel.setShowBadge(true) //是否在久按桌面图标时显示此渠道的通知
                notificationManager?.createNotificationChannel(notificationChannel)
                isCreateChannel = true
            }
            builder = NotificationCompat.Builder(
                Utils.getApp(),
                channelId
            )
        } else {
            builder =
                NotificationCompat.Builder( Utils.getApp())
        }
        builder
            .setSmallIcon(R.drawable.icon_location)
            .setContentTitle("位置上传")
            .setContentText("正在后台运行")
            .setWhen(System.currentTimeMillis())
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setStyle(
                NotificationCompat.BigTextStyle().setBigContentTitle("位置上传")
                    .bigText("正在后台运行")
            )

        return builder.build().apply {
            flags = NotificationCompat.FLAG_NO_CLEAR
        }
    }
}